package com.work.sparseArray;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

@SuppressWarnings("all")
public class work {
    public static void main(String[] args) throws Exception {

        int chessArr1[][] = new int[11][11];
        chessArr1[1][2] = 1;
        chessArr1[2][3] = 2;
        chessArr1[4][5] = 2;
        // 输出原始的二维数组
        System.out.println("原始的二维数组~~");
        for (int[] row : chessArr1) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }


        //先统计一下有几个有效数据
        int sum = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArr1[i][j] != 0) {
                    sum++;
                }
            }
        }

        int[][] sparseArr = new int[sum + 1][3];
        sparseArr[0][0] = 11;
        sparseArr[0][1] = 11;
        sparseArr[0][2] = sum;


        int count = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArr1[i][j] != 0) {
                    count++;
                    sparseArr[count][0] = i;
                    sparseArr[count][1] = j;
                    sparseArr[count][2] = chessArr1[i][j];
                }
            }
        }


       /* save(sparseArr, count);*/
        int[][] sparseArr2 = getArr(count + 1, 3);


        System.out.println("********************");
        for (int[] ints : sparseArr2) {
            for (int anInt : ints) {
                System.out.print(anInt + "\t");
            }
            System.out.println();
        }

        System.out.println("********************");

        //转回去
        int[][] arr = new int[sparseArr[0][0]][sparseArr[0][1]];
        for (int i = 1; i <= count; i++) {
            arr[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
        }


        for (int[] ints : arr) {
            for (int anInt : ints) {
                System.out.print(anInt + "\t");
            }
            System.out.println();
        }

    }

    public static void save(int[][] sparseArr, int count) throws Exception {

        File file = new File("D:\\io\\test.txt"); //存放数组数据的文件
        FileWriter out = new FileWriter(file); //文件写入流
        for (int i = 0; i <= count; i++) {
            for (int j = 0; j < 3; j++) {
                out.write(sparseArr[i][j] + "\t");
            }
            out.write("\r\n");
        }
        out.close();
    }


    public static int[][] getArr(int hang, int lie) throws Exception {

        int[][] arr2 = new int[hang][lie];
        File file = new File("D:\\io\\test.txt");
        BufferedReader in = new BufferedReader(new FileReader(file)); //
        String line; //一行数据
        int row = 0;
        //逐行读取，并将每个数组放入到数组中
        //readLine()方法用于从此LineNumberReader流中读取一行数据。
        //split()方法将一个字符串分割为子字符串(以上面为例子tab作为分隔符)，然后将结果作为字符串数组返回。
        //parseInt()方法，可以将spring转成对应的int
        while ((line = in.readLine()) != null) {
            String[] temp = line.split("\t");
            for (int j = 0; j < temp.length; j++) {
                arr2[row][j]=Integer.parseInt(temp[j]);
            }
            row++;
        }
        in.close();
        return arr2;
    }
}